package com.shuda.business.data.controller;

import com.shuda.business.data.dto.DataProcessingEngineDTO;
import com.shuda.business.data.dto.DataProcessingEngineQueryDTO;
import com.shuda.business.data.service.DataProcessingEngineService;
import com.shuda.common.dto.PageDTO;
import com.shuda.common.dto.PageResult;
import com.shuda.common.result.Result;
import com.shuda.common.exception.BusinessException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据处理引擎控制器
 */
@RestController
@RequestMapping("/api/data/processing-engine")
@Tag(name = "数据处理引擎", description = "数据处理引擎接口")
public class DataProcessingEngineController {

    @Autowired
    private DataProcessingEngineService dataProcessingEngineService;

    @GetMapping("/collision-check/{dataId}")
    @Operation(summary = "撞库检测")
    public Result<Boolean> collisionCheck(
            @Parameter(description = "数据ID") @PathVariable Long dataId) {
        try {
            Boolean result = dataProcessingEngineService.collisionCheck(dataId);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/process/{dataId}")
    @Operation(summary = "进件处理")
    public Result<Boolean> processData(
            @Parameter(description = "数据ID") @PathVariable Long dataId) {
        try {
            Boolean result = dataProcessingEngineService.processData(dataId);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/field-mapping/page")
    @Operation(summary = "获取字段映射配置分页列表")
    public Result<PageResult<DataProcessingEngineDTO>> getFieldMappingPage(
            @Parameter(description = "分页参数") PageDTO pageDTO,
            @Parameter(description = "查询参数") DataProcessingEngineQueryDTO queryDTO) {
        try {
            PageResult<DataProcessingEngineDTO> result = dataProcessingEngineService.getFieldMappingPage(pageDTO, queryDTO);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/field-mapping/{id}")
    @Operation(summary = "获取字段映射配置详情")
    public Result<DataProcessingEngineDTO> getFieldMappingById(
            @Parameter(description = "配置ID") @PathVariable Long id) {
        try {
            DataProcessingEngineDTO result = dataProcessingEngineService.getFieldMappingById(id);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/field-mapping")
    @Operation(summary = "创建字段映射配置")
    public Result<Long> createFieldMapping(
            @Parameter(description = "字段映射配置信息") @RequestBody DataProcessingEngineDTO dto) {
        try {
            Long id = dataProcessingEngineService.createFieldMapping(dto);
            return Result.success(id);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/field-mapping/{id}")
    @Operation(summary = "更新字段映射配置")
    public Result<Void> updateFieldMapping(
            @Parameter(description = "配置ID") @PathVariable Long id,
            @Parameter(description = "字段映射配置信息") @RequestBody DataProcessingEngineDTO dto) {
        try {
            dataProcessingEngineService.updateFieldMapping(id, dto);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/field-mapping/{id}")
    @Operation(summary = "删除字段映射配置")
    public Result<Void> deleteFieldMapping(
            @Parameter(description = "配置ID") @PathVariable Long id) {
        try {
            dataProcessingEngineService.deleteFieldMapping(id);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/script-config/{channelId}")
    @Operation(summary = "获取脚本配置")
    public Result<String> getScriptConfig(
            @Parameter(description = "渠道ID") @PathVariable Long channelId) {
        try {
            String result = dataProcessingEngineService.getScriptConfig(channelId);
            return Result.success(result);
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/script-config/{channelId}")
    @Operation(summary = "保存脚本配置")
    public Result<Void> saveScriptConfig(
            @Parameter(description = "渠道ID") @PathVariable Long channelId,
            @Parameter(description = "脚本配置") @RequestBody String scriptConfig) {
        try {
            dataProcessingEngineService.saveScriptConfig(channelId, scriptConfig);
            return Result.success();
        } catch (BusinessException e) {
            return Result.error(e.getMessage());
        }
    }
}